package utils

import (
	"net/http"
	"strings"
)

func GetClientIP(r *http.Request) string {
	clientIP := r.Header.Get("X-Forwarded-For")
	if clientIP == "" {
		clientIP = r.Header.Get("X-Real-IP")
	}
	// 获取 IP 地址部分
	clientIPList := strings.Split(clientIP, ",")
	if len(clientIPList) > 0 {
		clientIP = strings.TrimSpace(clientIPList[0])
	}

	if clientIP == "" {
		clientIP = r.RemoteAddr
	}

	if clientIP == "" {
		return ""
	}
	// 获取 IP 地址部分
	ipParts := strings.Split(clientIP, ":")
	if len(ipParts) > 0 {
		clientIP = ipParts[0]
	}
	return clientIP
}
